package com.gmrz.uaf.protocol.v1.schema;

import com.gmrz.util.Strings;

public class AAID {
    private transient String vendorCode;
    private transient String authenticatorModel;
    private transient String aaguid;

    public String getVendorCode() {
        return this.vendorCode;
    }

    public void setVendorCode(String vendorCode) {
        this.vendorCode = vendorCode;
    }

    public AAID withVendorCode(String vendorCode) {
        this.vendorCode = vendorCode;
        return this;
    }

    public String getAuthenticatorModel() {
        return this.authenticatorModel;
    }

    public void setAuthenticatorModel(String authenticatorModel) {
        this.authenticatorModel = authenticatorModel;
    }

    public AAID withAuthenticatorModel(String authenticatorModel) {
        this.authenticatorModel = authenticatorModel;
        return this;
    }

    public String getAaguid() {
        return aaguid;
    }

    public void setAaguid(String aaguid) {
        this.aaguid = aaguid;
    }

    public AAID withAaguid(String aaguid) {
        this.aaguid = aaguid;
        return this;
    }

    public String format() {
        if (Strings.isNotBlank(this.vendorCode) && Strings.isNotBlank(this.authenticatorModel)) {
            return this.vendorCode + "#" + this.authenticatorModel;
        } else {
            return this.aaguid;
        }
    }

    public AAID parse(String formatted) {
        if (formatted.contains("#")) {
            String[] parts = formatted.split("#");
            if (parts.length != 2)
                throw new RuntimeException("Invalid AAID: " + formatted);
            validateAuthenticator(parts[0], parts[1]);
            this.vendorCode = parts[0];
            this.authenticatorModel = parts[1];
        } else {
            this.aaguid = formatted;
        }
        return this;
    }

    private void validateAuthenticator(String vendor, String model) {
    }

    public int hashCode() {
        int prime = 31;
        int result = 1;
        result = prime * result + (this.authenticatorModel == null ? 0 : this.authenticatorModel.hashCode());

        result = prime * result + (this.vendorCode == null ? 0 : this.vendorCode.hashCode());

        return result;
    }

    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        AAID other = (AAID) obj;
        if (this.authenticatorModel == null) {
            if (other.authenticatorModel != null)
                return false;
        } else if (!this.authenticatorModel.equals(other.authenticatorModel))
            return false;
        if (this.vendorCode == null) {
            if (other.vendorCode != null)
                return false;
        } else if (!this.vendorCode.equals(other.vendorCode))
            return false;
        return true;
    }

    public String toString() {
        return format();
    }
}
